import { updateOrderEndTimeApi } from "@/api/insuranceCompany";

import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";

export default function OperationHook(state: any) {
  const { formState, loading, emit, formDataRef, passData } = state;
  const { resetFields } = FormHook({ formState });

  const onFinish = () => {
    const params = {
      ...formState,
      insuranceCompanyIdList: passData.value.map((t: any) => t.id)
    };

    return updateOrderEndTimeApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };

  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };

  return {
    onFinish,
    onFinishFailed,
    resetHandle
  };
}
